/*
 * Copyright (c) 2007 by Chances.
 * $CVSHeader: station-imsp/commons/nettv/commons/utils/FileNameUtils.java,v 1.2 2008/12/26 10:14:21 zhouzhan Exp $
 * $Author: libf $
 * $Date: 2010/04/18 06:36:33 $
 * $Revision: 1.1 $
 */
package mxmf.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * Class comments.
 * 
 * @author <a href="mailto:libf@chances.com.cn">libf</a>
 */
public class FileNameUtils {
    public static final String SPLIT = "/";

    public static final String EXTSPLIT = ".";

    private FileNameUtils() {
    }

    public static String recense(String fileName) {
        return StringUtils.replace(fileName, "\\", SPLIT);

    }

    /**
     * 获取文件的相对路径.
     * 
     * @param relativePath
     *            配置的存放相对路径.
     * @param fileName
     *            文件名称
     * @return
     */
    public static String getRelativePath(String relativePath, String fileName) {
        relativePath = FileNameUtils.recense(relativePath);

        String data = DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd");
        StringBuilder path = new StringBuilder();
        if (!relativePath.startsWith(SPLIT)) {
            path.append(SPLIT);
        }

        path.append(relativePath);

        if (!relativePath.endsWith(SPLIT)) {
            path.append(SPLIT);
        }
        path.append(data).append(SPLIT).append(fileName);

        return path.toString();
    }

    /**
     * 获取文件的绝对路径.
     * 
     * @param root
     *            配置的根路径.
     * @param relativePath
     *            文件的相对路径.
     * @return
     */
    public static String getAbsolutePath(String root, String relativePath) {
        String path = recense(root);
        if (path.endsWith(SPLIT)) {
            path = path.substring(0, path.length() - 1);
        }

        if (!relativePath.startsWith(SPLIT)) {
            path = path + SPLIT;
        }

        path = path + relativePath;

        return path;
    }

    public static String getFileNamePath(String fullFileName) {
        String path = recense(fullFileName);
        int idx = path.lastIndexOf(EXTSPLIT);
        if (idx > 0) {
            path = path.substring(0, idx);
        }
        return path;
    }

    /**
     * 获取文件名称.
     * 
     * @param fileName
     * @return
     */
    public static String getName(String fileName) {
        fileName = FileNameUtils.recense(fileName);
        int idx = fileName.lastIndexOf(SPLIT);
        if (idx > 0) {
            return fileName.substring(idx + 1);
        } else {
            return fileName;
        }
    }

    /**
     * 取得文件扩展名.
     * 
     * @param fileName
     * @return
     */
    public static String getExtName(String fileName) {
        fileName = FileNameUtils.recense(fileName);
        int idx = fileName.lastIndexOf(SPLIT);
        if (idx > 0) {
            fileName = fileName.substring(idx + 1);
        }

        idx = fileName.lastIndexOf(EXTSPLIT);

        if (idx > 0) {
            return fileName.substring(idx + 1);
        } else {
            return "";
        }
    }

    /**
     * 
     * @param path
     * @param name
     * @return
     */
    public static String join(String path, String name) {
        String aName = name.startsWith("/") ? name.substring(1) : name;
        if (path.endsWith("/")) {
            return path + aName;
        } else {
            return path + "/" + aName;
        }
    }

    /**
     * 
     * @param fileName
     * @return
     */
    public static String getPath(String fileName) {
        fileName = FileNameUtils.recense(fileName);
        int idx = fileName.lastIndexOf(SPLIT);
        if (idx > 0) {
            return fileName.substring(0, idx);
        } else {
            return fileName;
        }
    }

    /**
     * 判断文件是否为图片文件(GIF,PNG,JPG)
     * 
     * @param srcFileName
     * @return
     * @throws IOException 
     * @throws  
     */
    public static boolean isImage(String srcFileName) throws IOException {

        byte[] b = new byte[10];
        int l = -1;
        try (FileInputStream imgFile = new FileInputStream(srcFileName)) {
            l = imgFile.read(b);
        }
        if (l == 10) {
            byte b0 = b[0];
            byte b1 = b[1];
            byte b2 = b[2];
            byte b3 = b[3];
            byte b6 = b[6];
            byte b7 = b[7];
            byte b8 = b[8];
            byte b9 = b[9];
            if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
                return true;
            } else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
                return true;
            } else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'
                    && b9 == (byte) 'F') {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    public static String getFileContent(String path) {
        File file = new File(path);
        if (!file.exists() || file.isDirectory()) {
            return null;
        }

        try (FileInputStream in = new FileInputStream(file)) {

            byte[] bytes = new byte[1024];
            StringBuilder sb = new StringBuilder();
            while (in.read(bytes) != -1) {
                sb.append(new String(bytes));
                bytes = new byte[1024];
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String getWebContent(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == '<') {
                sb.append("&lt;");
            } else if (ch == '>') {
                sb.append("&gt;");
            } else if (ch == '&') {
                sb.append("&amp;");
            } else if (ch == '"') {
                sb.append("&quot;");
            } else if (ch == '\r') {
                sb.append("<br>");
            } else if (ch == '\n') {
                if (i > 0 && str.charAt(i - 1) == '\r') {

                } else {
                    sb.append("<br>");
                }
            } else if (ch == '\t') {
                sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
            } else if (ch == ' ') {
                sb.append("&nbsp;");
            } else if (ch == '<') {
                sb.append("&lt;");
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

}
